//<SCRIPT language="javascript" src="Imaging.js"></SCRIPT>
var selCustList = null;
var txtCFN = null;
var txtAID = null;
var selCFL = null;
var xmlCFL = null;
var rsCFL  = null;
var elEncode = null;
function load_Page_NS(){
	var div = document.createElement("DIV");
	div.id = "svcUo";
	document.body.appendChild(div);
	div.onreadystatechange = function(){onDivReadyChange(div)}
	div.addBehavior("../include/htc/webservice.htc");

	elEncode = document.createElement("DIV");
	document.body.appendChild(elEncode);

	txtCFN = F.txtApplID;
	//Locals
	function loadClosingFileList(){
		var result = uo.callService(cb,"GetClosingFileListXML");
		//Locals
		function cb(o){
			if( showSOAPError(o,elEncode) )return location.reload();
			xmlCFL = document.createElement("XML");
			document.body.appendChild(xmlCFL);
			xmlCFL.loadXML(o.value);
			rsCFL = xmlCFL.recordset;
			selCFL = document.createElement("SELECT");
			document.body.appendChild(selCFL);
			for( ; !rsCFL.EOF; rsCFL.moveNext() )
				selCFL[selCFL.length] = new Option(rsCFL("c").value,rsCFL("c").value);
			selCFL.size = Math.min(selCFL.length,8);
			selCFL.txtElement = txtCFN;
			txtCFN.selElement = selCFL;
			selCFL.onclick = function(){applID_changed(this.txtElement)};
			selCFL.onblur  = function(){hideTxtElement(this)};

			txtCFN.onfocus = function(){this.select();showClientList(this)};
			txtCFN.onblur = function(){hideTxtElement(this.selElement)};
			txtCFN.onpropertychange = function(){onCFNChanged(this)};
			txtCFN.onchange = function(){applID_changed(this)};
		}
	}
		//UO Web Service
	function onDivReadyChange(el){
		if( el.readyState != "complete" )return status = "Loading behavior ...";
		var WS_URL = 	location.protocol+"//"+location.host + "/WS/UO.asmx?WSDL";//"/WS/UO.asmx?WSDL";
		el.onserviceavailable = onUOAvailable
		el.useService(WS_URL,"UO");
	}
	function onUOAvailable(){
		uo = svcUo.UO;
		status = "UO behavior is loaded now.";
		loadClosingFileList();
	}
}
function menu_Page_NS(menu){
	var a = new Array;
	menu.addMenu("New Client","addClient()");
	menu.menus.unshift(menu.menus.pop());
}
function addClient(){
	var closingFile="";
	if( !(closingFile=prompt("Closing file name, pleace:","")) )return;
	var co = svcUo.createCallOptions("AddApplicant",undefined,false);
	var result = uo.callService(co,closingFile);
	if( showSOAPError(result,elEncode) )return;
	_applID(result.value);
}
// Event handlers
	//********	 C L F		*********
function onCFNChanged(txt){if( event.propertyName == "value" )findCFLItem(txt);}
function findCFLItem(txt){
	if( txt.value ){
		var re = new RegExp(txt.value,"i");
		for( rsCFL.moveFirst(); !rsCFL.EOF; rsCFL.moveNext() )
			if( rsCFL("c").value.match(re) )
				return selCFL.selectedIndex = rsCFL.absolutePosition-1;
	}
	selCFL.selectedIndex = -1;
}
function setTxtElement(sel){
	if( sel.selectedIndex >= 0 )sel.txtElement.value = sel.value;
	hideTxtElement(sel);
}
function hideTxtElement(sel){
	setTimeout(function(){sel.style.display = "none"},100);
}
function showClientList(txt){
	sel = selCFL;
	sel.style.display = "";
	sel.style.position = "absolute";
	var l = getLeftPos(txt);
	var t = getTopPos(txt);
	sel.style.posLeft = l+2;
	sel.style.posTop = t+txt.offsetHeight+2;
	sel.style.width = txt.offsetWidth;
	findCFLItem(txt);
}
	//*************************************
function applID_changed(txt){
	ff.select(0,true);
	ff.showFiles(divFolders,false);
	setTxtElement(txt.selElement);
	if( !txt.value )return;
	var co = svcUo.createCallOptions("GetApplIDByClosingFile",undefined,false);
	var result = uo.callService(co,txt.value);
	if( showSOAPError(result,elEncode) ){
		_applID(null);
		return txt.focus();
	}
	_applID(result.value);
}
// Helpers
function _CFN(v){
	return defined(v) ? txtCFN.value = v : txtCFN.value;
}
function _applID(v){
	if( defined(v) )txtCFN.applID = v;
	return txtCFN.applID || 0;
}
